home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13080 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  60 lines

  1. Path: isonews.bbn.hp.com!hpbblb!news
  2. From: Matthias Dittrich <matti>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with strcmp
  5. Date: 4 Apr 1996 12:41:08 GMT
  6. Organization: Hewlett-Packard Co.
  7. Message-ID: <4k0g14$as0@hpbblb.bbn.hp.com>
  8. References: <4jpiek$lp6@blaze.cs.jhu.edu>
  9. NNTP-Posting-Host: trabant.bbn.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
  14. X-URL: news:4jpiek$lp6@blaze.cs.jhu.edu
  15.  
  16. lasher@hops.cs.jhu.edu (John E. Davis) wrote:
  17. >For some reason when I try to compare the strings in the following 
  18. >snippet I consistently get a core dump (running on a UNIX machine running 
  19. >Solaris).  Could anyone point out what may be going wrong?  I have run it 
  20. >through the debugger and that was no help at all for me.  here is the 
  21. >code snippet:
  22. >
  23. >#include <stdio.h>
  24. >#include <stdlib.h>
  25. >#include <string.h>
  26. >
  27. >void setup(FILE *);
  28. >
  29. >void main(int argc, char *argv[])
  30. >{
  31. >char         buf[20], data[40], *buff, *arg, *fp;
  32. >FILE         *handle, *dest;
  33. >int         n;
  34. >
  35. >handle = fopen(argv[1], "r");
  36. >if(dest = fopen( "dbuild.out", "w")) setup(dest);
  37. >
  38. >while(!feof(handle)) {
  39. >    n = 0;
  40. >    fp = fgets(data, 40, handle);
  41. >    if ( strcasecmp(fp, "<action>\n") == 0)  /* the coredump is here */
  42. >        parseAction(handle, dest); 
  43. >    else if ( strcasecmp(fp, "<control>\n") == 0)
  44. >        parseControl(handle, dest);
  45. >    else if ( strcasecmp(fp, "<general>\n") == 0)
  46. >        parseGeneral(handle, dest); 
  47. >    printf("%s", data);
  48. >     }
  49. >fclose(dest);
  50. >
  51. >}
  52. >
  53. fgets returns a NULL-pointer if EOF is reached. I think this causes the
  54. core dump.
  55. Check the return value before using the pointer.
  56.  
  57. Good luck,
  58. Matthias
  59.  
  60.